package com.llkj.qingnianguoxue.zhuanlan;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.ZhiBoChatAdapter;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.bean.ZhiBoDetailBean;
import com.llkj.qingnianguoxue.bean.ZhiBoGiftBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DialogUtil;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.MyShare;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 正在直播、直播回看
 */
public class ZhiBoDetailActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener, MyShare.ShareResultCallback {
    private LinearLayout llPlayVideo;//播放图标
    private ImageView ivPic;
    private TextView tvVideoDuration;//视频时长
    private TextView tvDetailTitle;//标题
    private RelativeLayout rlBottom;
    private EditText etChat;//內容
    private TextView btnSend;//发送

    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvChat;
    private ZhiBoChatAdapter zhiBoChatAdapter;
    private List<ZhiBoDetailBean.MessageBean> chatData;
    private String mDetailId;
    private int totalSize, mPage;
    private boolean isSendMessage;//是否正在发送
    private String mLiveUrl;//直播封面图片
    private String mDescription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhi_bo_detail);
        setTitle("正在直播", true, TITLE_TYPE_IMG, R.drawable.icon_back, true, TITLE_TYPE_IMG, R.drawable.icon_book_share);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        initView();
        if (bundle != null && bundle.containsKey("id")) {
            mDetailId = bundle.getString("id");
        }
        String type = bundle.getString("type");
        if (bundle != null && "2".equals(type)) {
//            rlBottom.setVisibility(View.GONE);
//            tvTitle.setText("直播回看");
        } else {
            handler.postDelayed(runnable, 5*1000);
        }
        onRefresh();
    }

    private void initView() {
        initStatusbar(TSATUSBAR_TYPE_BLACK_TEXT,rlTitle,R.color.gray_bar);
        FrameLayout titleView = (FrameLayout) findViewById(R.id.fl_title);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) titleView.getLayoutParams();
        params.width = DisplayUtil.getDensityWdith(this);
        params.height = params.width * 300 / 750;

        llPlayVideo = (LinearLayout) findViewById(R.id.llPlay);
        ivPic = (ImageView) findViewById(R.id.iv_pic);
        FrameLayout.LayoutParams picParam = (FrameLayout.LayoutParams) ivPic.getLayoutParams();
        picParam.height = params.height;
        tvVideoDuration  = (TextView) findViewById(R.id.tv_video_duration);
        tvDetailTitle = (TextView) findViewById(R.id.tv_detail_title);

        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvChat = (RecyclerView) findViewById(R.id.recyclerView);
        int padding = DisplayUtil.dip2px(this, 10);
        lvChat.setPadding(padding, 0, padding, 0);
        lvChat.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        rlBottom = (RelativeLayout) findViewById(R.id.rl_bottom);
        etChat = (EditText) findViewById(R.id.et_chat);
        btnSend = (TextView) findViewById(R.id.tv_send);

        chatData = new ArrayList<>();
//        zhiBoChatAdapter = new ZhiBoChatAdapter(R.layout.item_zhibo_chat, chatData);
        zhiBoChatAdapter = new ZhiBoChatAdapter(R.layout.item_course_evaluate, chatData);
        lvChat.setAdapter(zhiBoChatAdapter);

        refreshLayout.setOnRefreshListener(this);
        btnSend.setOnClickListener(this);

    }

    @Override
    protected void rightDoWhat() {
        super.rightDoWhat();
        if(StringUtils.isEmpty(mLiveUrl)){
            return;
        }
        String title = tvDetailTitle.getText().toString();
        String linkUrl = String.format(HttpMethod.SHARE_LIVE, mDetailId);
        String description = mDescription;
        DialogUtil.showShareDialog(this, title, description, mLiveUrl, linkUrl, this);
    }

    @OnClick({R.id.ivGift, R.id.llPlay, R.id.iv_pic})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_send://发送
                String content = etChat.getText().toString().trim();
                if (StringUtils.isEmpty(content)) {
                    ToastUtil.makeLongText(this, "请输入您想要说的话");
                    return;
                }
                isSendMessage = true;
                loadWaitDialog();
                HttpMethod.liveAddMessage(this, this, mDetailId, content);
                break;
            case R.id.ivGift://
                Intent intent = new Intent(this,GiftDialogActivity.class);
                List<ZhiBoGiftBean> giftDatas = new ArrayList<>();
                initGiftData(giftDatas);
                initGiftData(giftDatas);
                initGiftData(giftDatas);
                intent.putExtra(GiftFragment.GIFT_DATA, (Serializable) giftDatas);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onRefresh() {
        mPage++;
        getZhiBoData(true);
    }

    private void getZhiBoData(boolean isShow) {
        if (isShow) {
            loadWaitDialog();
        }
        HttpMethod.liveDetails(this, this, mDetailId, String.valueOf(mPage));
    }

    private void initGiftData(List<ZhiBoGiftBean> giftDatas){
        ZhiBoGiftBean giftBean = new ZhiBoGiftBean();
        giftBean.setGiftName("鲜花");
        giftBean.setPrice("0");
        giftBean.setUrl("http://qingchunguoxue.apptiming.com/Uploads/2017-09-01/59a92b8db5f01.jpg");
        giftDatas.add(giftBean);
        giftBean = new ZhiBoGiftBean();
        giftBean.setGiftName("桃花扇");
        giftBean.setPrice("10.00");
        giftBean.setUrl("http://qingchunguoxue.apptiming.com/Uploads/2017-09-01/59a92b8db5f01.jpg");
        giftDatas.add(giftBean);
        giftBean = new ZhiBoGiftBean();
        giftBean.setGiftName("杨贵妃");
        giftBean.setPrice("20.00");
        giftBean.setUrl("http://qingchunguoxue.apptiming.com/Uploads/2017-09-01/59a92b8db5f01.jpg");
        giftDatas.add(giftBean);
        giftBean = new ZhiBoGiftBean();
        giftBean.setGiftName("鲜花");
        giftBean.setPrice("0");
        giftBean.setUrl("http://qingchunguoxue.apptiming.com/Uploads/2017-09-01/59a92b8db5f01.jpg");
        giftDatas.add(giftBean);
        giftBean = new ZhiBoGiftBean();
        giftBean.setGiftName("桃花扇");
        giftBean.setPrice("0");
        giftBean.setUrl("http://qingchunguoxue.apptiming.com/Uploads/2017-09-01/59a92b8db5f01.jpg");
        giftDatas.add(giftBean);
        giftBean = new ZhiBoGiftBean();
        giftBean.setGiftName("杨贵妃");
        giftBean.setPrice("20.00");
        giftBean.setUrl("http://qingchunguoxue.apptiming.com/Uploads/2017-09-01/59a92b8db5f01.jpg");
        giftDatas.add(giftBean);
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        refreshLayout.setRefreshing(false);
        if (method == HttpMethod.HTTP_LIVE_DETAILS) {
            ZhiBoDetailBean detailBean = GsonUtil.GsonToBean(data, ZhiBoDetailBean.class);
            if (detailBean != null) {
                if (mPage == 1) {
                    if(StringUtils.isEmpty(mLiveUrl)){
                        mLiveUrl = detailBean.getDetails().getImg();
                        try {
                            ImageLoadingUtil.loadingUrl(ivPic, mLiveUrl);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    tvDetailTitle.setText(detailBean.getDetails().getTitle());
                    mDescription = detailBean.getDetails().getAbstractX();
                }

                if (detailBean.getMessage() != null && detailBean.getMessage().size() > 0) {
                    if (mPage == 1) {
                        chatData.clear();
                        chatData.addAll(detailBean.getMessage());
                    } else {
                        chatData.addAll(0, detailBean.getMessage());
                    }
                    zhiBoChatAdapter.notifyDataSetChanged();
                    try {
                        if(chatData.size()>0){
                            lvChat.scrollToPosition(chatData.size()-1);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        } else if (method == HttpMethod.HTTP_LIVE_ADD_MESSAGE) {//7.4直播发表留言
            ZhiBoDetailBean detailBean = GsonUtil.GsonToBean(data, ZhiBoDetailBean.class);
            if (detailBean != null) {
                if (detailBean.getMessage() != null && detailBean.getMessage().size() > 0) {
                    chatData.clear();
                    chatData.addAll(detailBean.getMessage());
                    zhiBoChatAdapter.notifyDataSetChanged();
                    isSendMessage = false;
                    etChat.setText("");
                }
            }
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        super.onhttpFailed(message, method);
        refreshLayout.setRefreshing(false);
        if (!StringUtils.isEmpty(message)) {
            ToastUtil.makeLongText(this, message);
            isSendMessage = false;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        try {
            handler.removeCallbacks(runnable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void shareResultCallBack(int result) {
        switch (result) {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
        }
    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (!isSendMessage) {
                mPage = 1;
                getZhiBoData(false);
            }
            handler.postDelayed(this, 5 * 1000);
        }
    };
}
